#include "eeprom.h"
#include "i2c.h"
#include <stdio.h>

#define EEPROM_ADDR_START       0x0000
#define EEPROM_SIZE             23
static void i2c_start(void)
{
	i2c1_start();
}

static void i2c_send_byte(uint8_t byte)
{
	i2c1_send_byte(byte);
}

static uint8_t i2c_read_byte(void)
{
	return i2c1_read_byte();
}
static void i2c_stop(void)
{
	i2c1_stop();
}

static void i2c_ack(void)
{
	i2c1_ack();
}

static void i2c_wait_ack(void)
{
	i2c1_wait_ack();
}

static void i2c_nack(void)
{
	i2c1_nack();
}

void eeprom_write_bytes(uint8_t addr, uint8_t *buf, uint16_t len)
{
	i2c_start();
	i2c_send_byte(EEPROM_OPS_WRITE);
	i2c_wait_ack();
	i2c_send_byte(addr);
	i2c_wait_ack();
	while(len--)
	{
		i2c_send_byte(*buf++);
		i2c_wait_ack();
	}
	i2c_stop();
}

void eeprom_read_bytes(uint8_t addr, uint8_t *buf, uint16_t len)
{
	i2c_start();
	i2c_send_byte(EEPROM_OPS_WRITE);
	i2c_wait_ack();
	i2c_send_byte(addr);
	i2c_wait_ack();
	i2c_start();
	i2c_send_byte(EEPROM_OPS_READ);
	i2c_wait_ack();
	while(--len)
	{
		*buf++ = i2c_read_byte();
		i2c_ack();
	}
	*buf++ = i2c_read_byte();
	i2c_nack();
	i2c_stop();
}

uint8_t storage_params_check(void)
{
    uint8_t isInite[2] = {0x72, 0x50};
    uint8_t readTab[2];
    uint8_t ret = 0;
    eeprom_read_bytes(EEPROM_SIZE, readTab, 2);
    if((readTab[0] == isInite[0]) && (readTab[1] == isInite[1]))
    {
        ret = 1;
    }
    return ret;
}

void storage_params_save(Controller *ctrler)
{
    uint8_t saveTab[EEPROM_SIZE];
    uint8_t saveLen = 0;
    uint8_t isInite[2] = {0x72, 0x50};
    
    saveTab[saveLen++] = ctrler->reg[REG_POWER_STATE].val;
    saveTab[saveLen++] = ctrler->reg[REG_MODE_AC].val;
    saveTab[saveLen++] = ctrler->reg[REG_TEMP_SET_AC].val;
    saveTab[saveLen++] = ctrler->reg[REG_TEMP_SET_DN].val;
    saveTab[saveLen++] = ctrler->reg[REG_SPEED_SET_AC].val;
    saveTab[saveLen++] = ctrler->reg[REG_SPEED_SET_FRESH].val;
    saveTab[saveLen++] = ctrler->reg[REG_LOW_TEMP_PROTECT].val;
    saveTab[saveLen++] = ctrler->reg[REG_ECO].val;
    saveTab[saveLen++] = ctrler->reg[REG_SPEED_CTRL].val;
    saveTab[saveLen++] = ctrler->reg[REG_MEMORY].val;
    saveTab[saveLen++] = ctrler->reg[REG_CALIB].val;
    saveTab[saveLen++] = ctrler->reg[REG_BACKLASH_UP].val;
    saveTab[saveLen++] = ctrler->reg[REG_BACKLASH_DOWN].val;
    saveTab[saveLen++] = ctrler->reg[REG_TEMP_SET_LIMIT_UP].val;
    saveTab[saveLen++] = ctrler->reg[REG_TEMP_SET_LIMIT_DOWN].val;
    saveTab[saveLen++] = ctrler->reg[REG_ACMODE_SELECT].val;
    saveTab[saveLen++] = ctrler->reg[REG_TEMP_UNIT].val;
    saveTab[saveLen++] = ctrler->reg[REG_SYSTEM].val;
    saveTab[saveLen++] = ctrler->reg[REG_SYSTEM_SELECT].val;
    saveTab[saveLen++] = ctrler->deviceAddr;
    saveTab[saveLen++] = ctrler->baudrate;
    saveTab[saveLen++] = ctrler->luminance;
    saveTab[saveLen++] = ctrler->relayMode;
    saveTab[saveLen++] = isInite[0];
    saveTab[saveLen++] = isInite[1];
    eeprom_write_bytes(EEPROM_ADDR_START, saveTab, saveLen);
}

void storage_params_read(Controller *ctrler)
{
    uint8_t readTab[EEPROM_SIZE];
    uint8_t readLen = 0;
    eeprom_read_bytes(EEPROM_ADDR_START, readTab, EEPROM_SIZE);
    ctrler->reg[REG_POWER_STATE].val = readTab[readLen++];
    ctrler->reg[REG_MODE_AC].val = readTab[readLen++];
    ctrler->reg[REG_TEMP_SET_AC].val = readTab[readLen++];
    ctrler->reg[REG_TEMP_SET_DN].val = readTab[readLen++];
    ctrler->reg[REG_SPEED_SET_AC].val = readTab[readLen++];
    ctrler->reg[REG_SPEED_SET_FRESH].val = readTab[readLen++];
    ctrler->reg[REG_LOW_TEMP_PROTECT].val = readTab[readLen++];
    ctrler->reg[REG_ECO].val = readTab[readLen++];
    ctrler->reg[REG_SPEED_CTRL].val = readTab[readLen++];
    ctrler->reg[REG_MEMORY].val = readTab[readLen++];
    ctrler->reg[REG_CALIB].val = readTab[readLen++];
    ctrler->reg[REG_BACKLASH_UP].val = readTab[readLen++];
    ctrler->reg[REG_BACKLASH_DOWN].val = readTab[readLen++];
    ctrler->reg[REG_TEMP_SET_LIMIT_UP].val = readTab[readLen++];
    ctrler->reg[REG_TEMP_SET_LIMIT_DOWN].val = readTab[readLen++];
    ctrler->reg[REG_ACMODE_SELECT].val = readTab[readLen++];
    ctrler->reg[REG_TEMP_UNIT].val = readTab[readLen++];
    ctrler->reg[REG_SYSTEM].val = readTab[readLen++];
    ctrler->reg[REG_SYSTEM_SELECT].val = readTab[readLen++];
    ctrler->deviceAddr = readTab[readLen++];
    ctrler->baudrate = readTab[readLen++];
    ctrler->luminance = readTab[readLen++];
    ctrler->relayMode = readTab[readLen++];
}

void eeprom_test(void)
{
    uint8_t sendByte = 0x12;
    uint8_t readByte = 0;
    eeprom_write_bytes(0x00, &sendByte, 1);
    eeprom_read_bytes(0x00, &readByte, 1);
}
